package com.yjc.hire.server.canal;

import com.alibaba.fastjson.JSON;
import com.yjc.canal.DataDictCanal;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.pojo.DataDictionary;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.stringtemplate.v4.ST;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/11
 * </p>
 */
//指定关联的表名
@CanalTable("data_dictionary")
@Component
public class DataDictSyncHelper extends BaseInfoProperties implements EntryHandler<DataDictCanal> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String DDKEY_PREFIX = DATA_DICTIONARY_LIST_TYPECODE + ":";
    @Override
    public void insert(DataDictCanal dataDictCanal) {
        String ddkey = DDKEY_PREFIX + dataDictCanal.getType_code();
        String ddListStr = stringRedisTemplate.opsForValue().get(ddkey);
        List<DataDictionary> redisDDList = null;
        if (StringUtils.isBlank(ddListStr)) {
            redisDDList = new ArrayList<>();
        }else {
            redisDDList = JSON.parseArray(ddListStr, DataDictionary.class);
        }
        redisDDList.add(covertDD(dataDictCanal));
        stringRedisTemplate.opsForValue().set(ddkey, JSON.toJSONString(redisDDList));
    }
    private DataDictionary covertDD(DataDictCanal dataDictCanal){
        DataDictionary dictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictCanal, dictionary);
        dictionary.setItemKey(dataDictCanal.getItem_key());
        dictionary.setItemValue(dataDictCanal.getItem_value());
        dictionary.setTypeCode(dataDictCanal.getType_code());
        dictionary.setTypeName(dataDictCanal.getType_name());
        return dictionary;
    }

    @Override
    public void delete(DataDictCanal dataDictCanal) {
        String ddkey = DDKEY_PREFIX + dataDictCanal.getType_code();
        String ddListStr = stringRedisTemplate.opsForValue().get(ddkey);
        List<DataDictionary> redisDDList = null;
        if (StringUtils.isBlank(ddListStr)) {
            //为空不需要删除
        }else {
            redisDDList = JSON.parseArray(ddListStr, DataDictionary.class);
            for (DataDictionary dictionary : redisDDList) {
                if (Objects.equals(dictionary.getId(), dataDictCanal.getId())) {
                    redisDDList.remove(dictionary);
                    break;
                }
            }
            stringRedisTemplate.opsForValue().set(ddkey, JSON.toJSONString(redisDDList));
        }
    }

    @Override
    public void update(DataDictCanal before, DataDictCanal after) {
        String ddkey = DDKEY_PREFIX + after.getType_code();
        String ddListStr = stringRedisTemplate.opsForValue().get(ddkey);
        List<DataDictionary> redisDDList = null;
        if (StringUtils.isBlank(ddListStr)) {

        }else {
            redisDDList = JSON.parseArray(ddListStr, DataDictionary.class);
            for (DataDictionary dictionary : redisDDList) {
                if (Objects.equals(dictionary.getId(), after.getId())) {
                    DataDictionary covertDD = covertDD(after);
                    redisDDList.remove(dictionary);
                    redisDDList.add(covertDD);
                    break;
                }
            }
            stringRedisTemplate.opsForValue().set(ddkey, JSON.toJSONString(redisDDList));
        }
    }
}
